import request from "@/utils/request";
import { clearEmpty } from "@/utils/grpc";
import { useUserStore } from "@/stores/modules/user";

import type { AxiosPromise } from "axios";
import type {
  DictLayEntryApi,
  DictLayEntryReq,
  DictLayEntryModel,
  DictLayEntryPage,
  DictLayEntryList,
  DictLayEntryKey,
  DictLayEntryKeyList,
  DictLayEntryMoveKey,
  DictLayEntryEnableKey,
  DictLayEntryCreateReq,
  DictLayEntryUpdateReq
} from "./types";

class DictLayEntryApiImpl implements DictLayEntryApi {
  getKey(model: DictLayEntryModel): DictLayEntryKey {
    return {
      dictId: model.dictId
    } as DictLayEntryKey;
  }

  //查询翻页信息层级字典实体
  findPage(query?: DictLayEntryReq): AxiosPromise<DictLayEntryPage> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/list/" + tenant,
      method: "get",
      params: query
    });
  }

  //查询列表信息层级字典实体
  findList(query?: DictLayEntryReq): AxiosPromise<DictLayEntryList> {
    const tenant = useUserStore().user.tenant;
    if (query) {
      query.hasEnable = query.enable != undefined;
    }
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/list/" + tenant,
      method: "get",
      params: query
    });
  }

  //查询层级字典实体
  findById(query: DictLayEntryKey): AxiosPromise<DictLayEntryModel> {
    const tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/id/" + tenant + "/" + query.dictId,
      method: "get"
    });
  }

  // 创建实体层级字典实体
  create(data: DictLayEntryCreateReq): AxiosPromise<DictLayEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry",
      method: "post",
      data: data
    });
  }

  //修改实体层级字典实体
  update(data: DictLayEntryUpdateReq): AxiosPromise<DictLayEntryModel> {
    data.tenant = useUserStore().user.tenant;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry",
      method: "patch",
      data: data
    });
  }

  //上移下移
  move(key: DictLayEntryKey, up: boolean): AxiosPromise<boolean> {
    const data: DictLayEntryMoveKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.up = up;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/move",
      method: "put",
      data: data
    });
  }

  //启用/禁用
  enable(key: DictLayEntryKey, enable: boolean): AxiosPromise<boolean> {
    const data: DictLayEntryEnableKey = { ...key };
    data.tenant = useUserStore().user.tenant;
    data.enable = enable;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/enable",
      method: "put",
      data: data
    });
  }

  // 删除层级字典实体
  delete(query: DictLayEntryKey): AxiosPromise<DictLayEntryModel> {
    query.tenant = useUserStore().user.tenant;
    clearEmpty(query);
    return request({
      url: "/v1/dict/dict_lay_entry/id",
      method: "delete",
      params: query
    });
  }

  // 批量删除层级字典实体
  batchDelete(keys: DictLayEntryKey[]): AxiosPromise<number> {
    const ids = keys.map((item) => item.dictId);
    const data = {
      tenant: useUserStore().user.tenant,
      dictId: ids
    } as DictLayEntryKeyList;
    clearEmpty(data);
    return request({
      url: "/v1/dict/dict_lay_entry/batch",
      method: "put",
      data: data
    });
  }
}

const dictLayEntryApi = new DictLayEntryApiImpl();

export { dictLayEntryApi };
